// pages/technology/technology.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picUrl: '',
    picPath: "",
    info: '未认证',
    isCheck: 0,
    refuseReason: "",
    detailPics: []
  },

  /**
   * 上传图片事件函数
  //  */
  // upload: function () {
  //   let that = this;
  //   wx.chooseImage({
  //     count: 1,
  //     // sizeType: ['original', 'compressed'],
  //     success: function (res) {
  //       console.log(res);

  //       var img = res.tempFilePaths[0];
  //       that.setData({
  //         picPath: img
  //       })

  //       wx.uploadFile({
  //         url: 'https://www.xiaozheng8.com/upload',
  //         filePath: that.data.picPath,
  //         name: 'file',
  //         header: {
  //           'Content-Type': 'multipart/form-data'
  //         },
  //         success: function (res) {
  //           console.log(res);
  //           // var picPath=JSON.parse(res.data);
  //           // console.log(picPath);
  //           that.setData({
  //             picPath: res.data
  //           })
  //           console.log(that.data.picPath);
  //         },
  //       })

  //     }


  //   })

  // },


  // /**
  //  * 删除图片事件函数
  //  */
  // cancel: function () {
  //   this.setData({
  //     picPath: "",
  //     picUrl: ""
  //   })
  // },

  /**
   * 提交事件
   */
  submit: function() {
    let that = this;
    if (this.data.detailPics == "") {
      wx.showToast({
        title: '请上传图片',
        icon: "none"
      })
    } else {
      wx.showModal({
        title: "提示",
        content: '确认提交',
        success: function(res) {
          if (res.confirm) {
            wx.request({
              url: 'https://www.xiaozheng8.com/auth/skill',
              method: 'post',
              data: {
                userId: wx.getStorageSync("userId"),
                skillCertificate: that.data.detailPics
              },
              success: function(res) {
                console.log(res);
                // debugger
                if (res.data.code == 200) {
                  // that.setData({
                  //   info: '已认证',
                  //   picUrl: that.data.picPath,
                  // })
                  console.log(that.data.picUrl);
                  wx.showModal({
                    title: '提示',
                    content: '信息提交成功',
                    showCancel: false,
                    success: function(res) {}
                    // if (res.confirm) {
                    // var pages = getCurrentPages();
                    // var currPage = pages[pages.length - 1]; //当前页面
                    // var prevPage = pages[pages.length - 2]; //上一个页面

                    // prevPage.setData({
                    //   book: 1
                  });

                } else {
                  wx.showModal({
                    title: '提示',
                    content: '信息提交失败，请重新上传',
                    showCancel: false,
                    success: function(res) {}
                  })
                }
              }
            })


            //     }
            //   }
            // })

            wx.navigateBack({

            })
          }
        },
      })
    }





  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    wx.request({
      url: 'https://www.xiaozheng8.com/auth/skillFind',
      method: 'post',
      data: {
        userId: wx.getStorageSync("userId")
      },
      success: function(res) {
        console.log(res);
        console.log(res.data.data.skillCertificate)
        that.setData({
          picPath: res.data.data.skillCertificate,
          picUrl: res.data.data.skillCertificate,
          isCheck: res.data.data.isCheck,
          refuseReason: ""
        })
        // var urlList=that.data.picUrl
        console.log(that.data.picUrl.split("@"))
        var urlList = that.data.picUrl.split("@");
        for(var i=0;i<urlList.length;i++){
          if(urlList[i]==""){
            urlList.splice(i,1)
          }
        }
        console.log(urlList);
        that.setData({
          detailPics:urlList
        })
        console.log(that.data.isCheck)
      }
    })
  },
  // 预览图片
  preview(e) {
    var that = this;
    var index = e.currentTarget.dataset.index
    wx.previewImage({
      current: that.data.detailPics[index],
      urls: that.data.detailPics,

    })
  },
  /**
   * 删除图片事件函数
   */
  // cancel: function (e) {
  //       let arr = this.data.imageSrc;
  //       let a = e.currentTarget.dataset.index
  //       this.data.imageSrc[a] = ''
  //       this.setData({
  //             imageSrc: this.data.imageSrc
  //       })
  // },
  // cancel: function (e) {
  //   let arr = this.data.imageSrc;
  //   let a = e.currentTarget.dataset.index
  //   var that = this;
  //   if (this.data.imageSrc.length > 1) {
  //     that.data.imageSrc.splice(a, 1);
  //     that.setData({
  //       imageSrc: that.data.imageSrc
  //     })
  //   } else {
  //     that.data.imageSrc[a] = "../../icons/jia.png",
  //       that.setData({
  //         imageSrc: that.data.imageSrc
  //       })
  //   }


  // },

  uploadDetailImage: function(e) { //这里是选取图片的方法
    var that = this;
    var pics = [];
    var picList = [];
    var detailPics = that.data.detailPics;
    if (detailPics.length >= 9) {
      wx.showToast({
        title: '最多选择' + 9 + '张！',
        icon: "none"
      })
      return;
    }

    wx.chooseImage({
      count: 9 - this.data.detailPics.length, // 最多可以选择的图片张数，默认9
      sizeType: ['compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function(res) {
        var imgs = res.tempFilePaths;
        console.log(imgs);
        console.log(res);
        // for (var i = 0; i < imgs.length; i++) {
        //   pics.push(imgs[i])
        // }
        console.log(pics)
        // if (res.tempFileSize < 3000000) {
        console.log(imgs);
        pics = imgs;
        for (var i = 0; i < pics.length; i++) {
          if (res.tempFiles[i].size > 5000000) {
            wx.showToast({
              title: '图片上传失败',
              icon: "none"
            })
          }
        }

        console.log(pics);

        for (let i = 0; i < pics.length; i++) {
          wx.uploadFile({
            url: 'https://www.xiaozheng8.com/upload',
            filePath: pics[i],
            name: 'file',
            header: {
              'Content-Type': 'multipart/form-data'
            },
            success: function(res) {

              that.data.detailPics.push(res.data);
              console.log(res.data);
              that.setData({
                detailPics: that.data.detailPics
              })
              if (that.data.detailPics.length > 9) {
                wx.showToast({
                  title: '最多选择' + 9 + '张！',
                  icon: "none"
                })
                that.data.detailPics.splice(9, that.data.detailPics.length - 9)
                that.setData({
                  detailPics: that.data.detailPics
                })
              }
              console.log(that.data.detailPics)

            }

          })

        }

        console.log(that.data.detailPics)

      },
    })

  },

  cancel: function(e) {
    var index = e.currentTarget.dataset.index;
    console.log(index);
    this.data.detailPics.splice(index, 1);
    this.setData({
      detailPics: this.data.detailPics
    })
  },


  /**
   * 提交报告
   */
  // submit: function() {
  //   let that = this;
  //   console.log(that.data.imageSrc);
  //   console.log(that.data.detailPics)
  //   if (that.data.detailPics == '') {
  //     wx.showModal({
  //       title: "提示",
  //       content: '请上传图片',
  //       showCancel: false,
  //       confirmText: '确认',
  //       confirmColor: '#0bbd79',
  //     })
  //   } else {
  //     wx.showModal({
  //       title: "提示",
  //       content: '确认提交',
  //       cancelText: '取消',
  //       cancelColor: '#999',
  //       confirmText: '确认',
  //       confirmColor: '#0bbd79',
  //       success: function(res) {
  //         if (res.confirm) {
  //           var pages = getCurrentPages();
  //           var prevPage = pages[pages.length - 2]; //上一个页面
  //           //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
  //           prevPage.setData({
  //             sceneImg: that.data.detailPics
  //           })
  //           wx.navigateBack({ //返回
  //             delta: 1
  //           })
  //         } else {}

  //       }
  //     })

  //   }

  // },


  /**
   * 生命周期函数--监听页面加载
   */

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },



  // /**
  //  * 生命周期函数--监听页面初次渲染完成
  //  */
  // onReady: function () {

  // },

  // /**
  //  * 生命周期函数--监听页面显示
  //  */
  // onShow: function () {

  // },

  // /**
  //  * 生命周期函数--监听页面隐藏
  //  */
  // onHide: function () {

  // },

  // /**
  //  * 生命周期函数--监听页面卸载
  //  */
  // onUnload: function () {

  // },

  // /**
  //  * 页面相关事件处理函数--监听用户下拉动作
  //  */
  // onPullDownRefresh: function () {

  // },

  // /**
  //  * 页面上拉触底事件的处理函数
  //  */
  // onReachBottom: function () {

  // },

  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage: function () {

  // }
})